from PyQt4 import QtCore, QtGui
import os
import dbus

class QRcodeGUI(QtGui.QMainWindow):
    IP = None
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('LinRemote authorization.')
        self.setWindowIcon(QtGui.QIcon(os.getcwd()+'/tray.png'))
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setStyleSheet("background-color:white;")
        self.w = QtGui.QWidget()
        self.w.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.toolbar = QtGui.QToolBar()
        self.toolbar.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.left_spacer = QtGui.QWidget()
        self.left_spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.closeButton = QtGui.QPushButton(QtGui.QIcon.fromTheme('window-close'), '&Close')
        self.ignoreButton = QtGui.QPushButton(QtGui.QIcon.fromTheme("list-remove-user"), '&Ignore this IP')
        self.right_spacer = QtGui.QWidget()
        self.right_spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.buttonHtoolbar = QtGui.QToolBar()
        self.buttonHtoolbar.setOrientation(QtCore.Qt.Horizontal)
        self.buttonToolbar = QtGui.QToolBar()
        self.buttonToolbar.setOrientation(QtCore.Qt.Vertical)
        self.buttonToolbar.addWidget(self.ignoreButton)
        self.buttonToolbar.addWidget(self.closeButton)
        self.buttonHtoolbar.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.buttonHtoolbar.addWidget(self.right_spacer)
        self.buttonHtoolbar.addWidget(self.buttonToolbar)
        self.setCentralWidget(self.w)
        self.qrcode = QtGui.QLabel('No QR code to display...')
        self.qrcode.setScaledContents(False)
        self.qrcode.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.layout = QtGui.QVBoxLayout(self.w)
        self.toolbar.addWidget(self.left_spacer)
        self.toolbar.addWidget(self.qrcode)
        self.toolbar.addWidget(self.buttonHtoolbar)
        self.layout.addWidget(self.toolbar)
        self.showFullScreen()
        
        QtCore.QObject.connect(self.closeButton, QtCore.SIGNAL('clicked()'), self.close)
        QtCore.QObject.connect(self.ignoreButton, QtCore.SIGNAL('clicked()'), self.ignoreThisIP)
        
        self.installEventFilter(self)
        self.hide()

    def closeEvent(self, event):
        self.qrcode.setText('No QR code to display...')
        self.hide()
        event.ignore()

    def showWindow(self, code, IP):
        self.IP = IP
        self.setWindowTitle('LinRemote authorization. IP: ' + IP)
        self.ignoreButton.setText('Ignore: ' + IP)
        self.showFullScreen()
        self.activateWindow()
        getattr(self, "raise")()
        self.setFocus(True)
        self.changeImage(code)

    def changeImage(self, code):
        self.qrcode.setPixmap(QtGui.QPixmap(code).scaledToHeight(self.qrcode.height()))

    def removeImage(self):
        self.qrcode.setText('No QR code to display...')
        self.hide()
        
    def ignoreThisIP(self):
        try:
            bus = dbus.SessionBus()
            app = 'org.LinRemote.Server'
            path = '/org/LinRemote/Server'
            interface = 'org.LinRemote.Server'
            obj = bus.get_object(app, path)
            DBusInterface = dbus.Interface(obj, interface)
            getattr(DBusInterface, 'addToIgnoreList')(self.IP)
        except Exception as msg:
            print(str(msg))
            self.qrcode.setText('Error... Server might not be running...')
            return
        self.qrcode.setText('In Your Face Bitch ' + self.IP + ' !!!')

    def eventFilter(self,  obj,  event):
        if event.type() == QtCore.QEvent.WindowDeactivate:
            self.qrcode.setText('No QR code to display...')
            self.hide()
            #self.qrcode.setText('No QR code to display...')
            return True
        return False